<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\ExcelExport\RuralExpoter;
use App\Admin\Extensions\Tools\ImportButton;
use App\Models\Area;
use App\Models\Rural;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class RuralController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('乡村旅游');
            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Rural::class, function (Grid $grid) {
            $grid->actions(function ($actions) {
                $actions->disableView();
            });
            $grid->tools(function ($tools) {
                $tools->append(new ImportButton('rurals'));
            });
            $grid->exporter(new RuralExpoter());
            $grid->filter(function($filter){
                // 去掉默认的id过滤器
                $filter->disableIdFilter();
                $filter->like('title','名称');
                $filter->equal('area_id','区县选择')->select(Area::all()->pluck('title', 'id'));
            });
            $grid->id('ID')->sortable();
            $grid->area_id('区县')->editable('select', Area::all()->pluck('title','id'));
            $grid->title('名称');
            $grid->name('负责人/联系人');
            $grid->tel('电话');
            $grid->updated_at('更新于');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Rural::class, function (Form $form) {

            $form->display('id', 'ID');
            $form->select('area_id','区县')->options(Area::all()->pluck('title','id'));
            $form->text('title','名称');
            $form->text('name','负责人/联系人')->default('暂无');
            $form->text('address','地址');
            $form->text('tel','联系电话');
        });
    }
}
